<!--
 * @Author: MrDotYan 2564217790@qq.com
 * @Date: 2024-09-24 14:14:55
 * @LastEditors: MrDotYan 2564217790@qq.com
 * @LastEditTime: 2024-09-25 21:54:46
 * @FilePath: /webServerAdmin/src/views/system/notice/components/edit.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <el-drawer size="48vw" v-model="visible" title="通知/消息编辑">
    <el-form ref="formRef" :model="modelValue" :rules="rules" label-position="top">
      <el-form-item label="消息/通知标题" prop="title">
        <el-input v-model="modelValue.title"></el-input>
      </el-form-item>
      <el-form-item label="消息/通知内容" prop="context">
        <WangEditor v-model:value="modelValue.context"></WangEditor>
      </el-form-item>
    </el-form>
    <template #footer>
      <div>
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-drawer>
</template>

<script lang="ts" setup>
import { editNotice, fetchNoticeInfo, saveNotice } from "@/api/modules/notice";
import WangEditor from "@/components/WangEditor/index.vue";
import { ResultEnum } from "@/enums/httpEnum";
import { NoticeSchema } from "@/schema";
import { EmitProps } from "@/typings/interface";
import { ElNotification, FormInstance } from "element-plus";
import { nextTick } from "vue";
import { watch } from "vue";
import { reactive } from "vue";
import { ref } from "vue";

const visible = ref(false);
const formRef = ref<FormInstance>();
const modelValue = reactive<NoticeSchema>({
  title: "",
  context: ""
});

const rules = {
  title: [{ required: true, message: "请输入消息/通知标题", trigger: "blur" }],
  context: [{ required: true, message: "请输入消息/通知内容", trigger: "blur" }]
};

function submit() {
  formRef.value?.validate().then(valid => {
    if (valid) {
      if (modelValue.id) {
        edit();
      } else {
        save();
      }
    }
  });
}

const emit = defineEmits<EmitProps>();
function save() {
  saveNotice(modelValue).then(r => {
    const { code, message } = r;
    if (code == ResultEnum.SUCCESS) {
      emit("success");
      visible.value = false;
    } else {
      ElNotification.error(message);
    }
  });
}

function edit() {
  editNotice({ id: modelValue.id }, modelValue).then(r => {
    const { code, message } = r;
    if (code == ResultEnum.SUCCESS) {
      emit("success");
      visible.value = false;
    } else {
      ElNotification.error(message);
    }
  });
}

watch(
  () => visible.value,
  newVal => {
    if (!newVal) {
      formRef.value?.resetFields();
      modelValue.id = "";
    }
  }
);

defineExpose({
  open(row) {
    if (row && row.id) {
      fetchNoticeInfo({ id: row.id }).then(r => {
        const { code, message, data } = r;
        if (code === ResultEnum.SUCCESS) {
          nextTick(() => {
            Object.assign(modelValue, data);
          });
        } else {
          ElNotification.error(message);
        }
      });
    }
    visible.value = true;
  }
});
</script>
